﻿using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;
using System.Diagnostics;


/// <summary>
/// Descripción breve de ptss
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class ptss : System.Web.Services.WebService
{

    public ptss()
    {

        //Eliminar la marca de comentario de la línea siguiente si utiliza los componentes diseñados 
        //InitializeComponent(); 
    }

    [WebMethod]
    public List<Tournament> GetTournaments()
    {
        return Tournament.getAvailableTournaments();
    }

    [WebMethod]
    public Boolean GetIfTournamentsIsInQualify(int p_Pk_Tournament)
    {
        List<Player> List = GetPlayers(p_Pk_Tournament, false);

        if (List != null)
        {
            if (List.Count > 0)
            {
                return false;
            }
            else {
                return true;
            }
        }
        else {
            return true;
        } 

        
    }

    [WebMethod]
    public List<Player> GetPlayers(int p_Pk_Tournament, Boolean IsQualify)
    {
       
        if (IsQualify == true)
        {
            return Player.GetPlayersByTournament(p_Pk_Tournament);
        }
        else { 
            PlayoffRounds oPlayoffRounds = new PlayoffRounds(p_Pk_Tournament);
            return oPlayoffRounds.GetPlayersForTournament(p_Pk_Tournament);
        
        }
    }

    [WebMethod]
    public List<Machine> GetMachinesByPlayerAutoDetectRound(int p_Pk_Tournament, int p_Pk_Player)
    {

        if (GetIfTournamentsIsInQualify(p_Pk_Tournament) == true)
        {
            return Machine.GetQualifyMachinesByPlayer(p_Pk_Player, p_Pk_Tournament);
        }
        else
        {
            PlayoffRounds oPlayoffRounds = new PlayoffRounds(p_Pk_Tournament);
            return oPlayoffRounds.GetMachinesForPlayer(p_Pk_Player);
        }
    }

    [WebMethod]
    public List<Machine> GetMachinesByPlayer(int p_Pk_Tournament, int p_Pk_Player, Boolean IsQualify)
    {
        
        if(IsQualify==true){
        return Machine.GetQualifyMachinesByPlayer(p_Pk_Player, p_Pk_Tournament);
        }else{
            PlayoffRounds oPlayoffRounds = new PlayoffRounds(p_Pk_Tournament);
             return oPlayoffRounds.GetMachinesForPlayer(p_Pk_Player); 
        }
    }
   
    [WebMethod]
    public Int64 GetPlayerScore(int p_Pk_Tournament, int p_Pk_Player, int p_Pk_Machine, Boolean IsQualify)
    {
        try
        {
            if (IsQualify == true)
            {
                return QualifyRounds.GetScore(p_Pk_Tournament, p_Pk_Player, p_Pk_Machine, IsQualify);
                
            }
            else
            {
                return PlayoffRounds.GetScore(p_Pk_Tournament, p_Pk_Player, p_Pk_Machine,true);
            }

        }
        catch (Exception Ex)
        {
            return -2;
        }
    }

    [WebMethod]
    public Boolean InsertPlayerScore(int p_Pk_Tournament, int p_Pk_Player, int p_Pk_Machine, Int64 p_Score, Boolean IsQualify, Boolean IsJoker, out String Error)
    {
        Error = "None";
        try
        {
            if (IsQualify == true)
            {
                Boolean res = QualifyRounds.InsertScores(p_Pk_Tournament, p_Pk_Player, p_Pk_Machine, p_Score, true, IsJoker, out Error);
                return res;
            }
            else {
                Boolean res = PlayoffRounds.InsertScores(p_Pk_Tournament, p_Pk_Player, p_Pk_Machine, p_Score, false, IsJoker, out Error);
                return res;
            }
            
        }
        catch (Exception Ex) {
            Error = Ex.ToString(); 
            return false;
        }
    }

    [WebMethod]
    public Boolean InsertPlayerScoreSimple(int p_Pk_Tournament, int p_Pk_Player, int p_Pk_Machine, Int64 p_Score, Boolean IsQualify, Boolean IsJoker)
    {
        string Error = "None";
        try
        {
            if (IsQualify == true)
            {
                Boolean res = QualifyRounds.InsertScores(p_Pk_Tournament, p_Pk_Player, p_Pk_Machine, p_Score, true, IsJoker, out Error);
                return res;
            }
            else
            {
                Boolean res = PlayoffRounds.InsertScores(p_Pk_Tournament, p_Pk_Player, p_Pk_Machine, p_Score, false, IsJoker, out Error);
                return res;
            }

        }
        catch (Exception Ex)
        {
            string sSource;
            string sLog;
            string sEvent;

            sSource = "PTSS";
            sLog = "INSERT PLAYER SCORE";
            sEvent = Ex.ToString();
            if (!EventLog.SourceExists(sSource))
                EventLog.CreateEventSource(sSource, sLog);
			EventLog.WriteEntry(sSource,sEvent,EventLogEntryType.Error);
		
            //Error = Ex.ToString();
            return false;
        }
    }
 

    //QualifyRounds

}

